package com.achievo.know.web.front;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.CategoryApplication;
import com.achievo.know.application.FrontApplication;
import com.achievo.know.application.LinkApplication;
import com.achievo.know.application.NewsApplication;
import com.achievo.know.domain.Area;
import com.achievo.know.domain.FriendLink;
import com.achievo.know.domain.News;
import com.achievo.know.domain.FriendLink.LinkType;
import com.achievo.know.pojo.AreaConstant;
import com.achievo.know.pojo.JsonCategory;
import com.achievo.know.pojo.SearchResult;
import com.achievo.know.web.util.PageActionSupport;
import com.dayatang.querychannel.support.Page;
import com.redhat.redwork.coc.RedworkAction;

public class IndexAction extends PageActionSupport implements RedworkAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6163635488534573917L;
	private String code;
	private String jsonResult;
	private int size=5;
	
	public boolean exists(List<News> list,News n){
		for(News t:list){
			if(t.getId()==n.getId()){
				return true;
			}
		}
		return false;
	}
	
	public String index(){
		List<Long> cs=null;
		if(StringUtils.isNotEmpty(getUserId() )){
			cs=getUserCateIds();
		}
		newsNoticeImage=new ArrayList<News>();
		cates=categoryApplication.getCates(cs);
		important=frontApplication.getArea(AreaConstant.IMPORTANT);
		if(important!=null){
			newsImport=frontApplication.findNewsByArea(cs,important.getId(),size);
			for(News n:newsImport){
				if(n.getNewsImage()!=null && n.getNewsImage().getImagePath()!=null){
					if(!exists(newsNoticeImage, n)){
						newsNoticeImage.add(n);
					}
				}
			}
		}
		tinicNews=frontApplication.getArea(AreaConstant.TICKNEWS);
		if(tinicNews!=null){
			newsTick=frontApplication.findNewsByArea(cs,tinicNews.getId(),size);
			for(News n:newsTick){
				if(n.getNewsImage()!=null && n.getNewsImage().getImagePath()!=null){
					if(!exists(newsNoticeImage, n)){
						newsNoticeImage.add(n);
					}
				}
			}
		}
		notice=frontApplication.getArea(AreaConstant.NOTICE);
		if(notice!=null){
			newsNotice=frontApplication.findNewsByArea(cs,notice.getId(),size);
			for(News n:newsNotice){
				if(n.getNewsImage()!=null && n.getNewsImage().getImagePath()!=null){
					if(!exists(newsNoticeImage, n)){
						newsNoticeImage.add(n);
					}
				}
			}
		}
		area4=frontApplication.getArea(AreaConstant.AREA4);
		if(area4!=null)
			newsArea4=frontApplication.findNewsByArea(cs,area4.getId(),size);
		outLink=linkApplication.findLinks(LinkType.OUT);
		inLink=linkApplication.findLinks(LinkType.IN);
		
		if(newsNoticeImage.size()>0){
			List<News> temp=new ArrayList<News>();
			for(News n:newsNoticeImage){
				temp.add(n);
			}
			Collections.sort(temp, new Comparator<News>(){

				@Override
				public int compare(News f, News n) {
					if(n.getPublishTime().after(f.getPublishTime()))
						return 1;
					else if(n.getPublishTime().before(f.getPublishTime()))
						return -1;
					else
						return 0;
				}
				
			});
			newsNoticeImage.clear();
			int i=1;
			for(News n:temp){
				newsNoticeImage.add(n);
				if(i++>=5)
					break;
			}
		}
		
		
		return "index";
	}
	
	public String more(){
		List<Long> cs=null;
		if(!isSuperAdmin()){
			cs=getUserCateIds();
		}
		mores=frontApplication.findPageNewsByArea(cs,Long.parseLong(type), currentPage,pageSize);
		cates=categoryApplication.getCates(cs);
		return "more";
	}
	
	public String areaList(){
		areas=frontApplication.listArea();
		return "areas_list";
	}
	
	public String newsListByArea(){
		Area temp=frontApplication.getArea(areaId);
		mores=frontApplication.findPageNewsByArea(null, temp.getId(), currentPage, pageSize);
		if(temp.getId()==AreaConstant.AREA4)
			return "news_list_area4";
		else
			return "news_list_area";
	}
	
	public String setSugest(){
		try{
			if(StringUtils.isNotEmpty(ids)){
				String[] idArr=ids.split(",");
				newsApplication.setSugest(idArr,sugestDate);
				jsonResult="设置成功!";
				code="1";
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult="出错："+e.getMessage();
			code="0";
		}
		return JSON;
	}
	
	public String editArea(){
		try{
			Area temp=frontApplication.getArea(areaId);
			temp.setName(important.getName());
			frontApplication.saveArea(temp);
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String showArea(){
		try{
			Area temp=frontApplication.getArea(areaId);
			temp.setShow(important.isShow());
			frontApplication.saveArea(temp);
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String changePosition(){
		try{
			News onew=newsApplication.get(id);
			News dnew=newsApplication.get(lid);
			if(onew!=null && dnew!=null){
				newsApplication.changePosition(onew,dnew);
			}
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String topPosition(){
		try{
			News onew=newsApplication.get(id);
			if(onew!=null){
				Long top=newsApplication.getTop(areaId);
				onew.setPosition((int) (top+1));
				newsApplication.updateNews(onew);
			}
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String topPosition1(){
		try{
			News onew=newsApplication.get(id);
			if(onew!=null){
				Long top=newsApplication.getTop();
				onew.setPosition((int) (top+1));
				newsApplication.updateNews(onew);
			}
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String search(){
		if(searchText!=null)
			searchText=searchText.trim();
		sResult=frontApplication.search(searchText,currentPage,pageSize);
		if(isSuperAdmin())
			cates=categoryApplication.getCates(null);
		else
			cates=categoryApplication.getCates(getUserCateIds());
		return "search-result";
	}
	
	@SuppressWarnings("deprecation")
	public boolean isToday(Date d){
		Date c=new Date();
		if(d.getYear()==c.getYear() && d.getMonth()==c.getMonth() && d.getDate()==c.getDate())
			return true;
		return false;
	}
	
	public int strLen(String str){
		if(StringUtils.isEmpty(str)){
			return 0;
		}
		return str.getBytes().length;
	}
	
	@Autowired
	private CategoryApplication categoryApplication;
	@Autowired
	private FrontApplication frontApplication;
	@Autowired
	private NewsApplication newsApplication;
	private List<JsonCategory> cates;
	private List<News> newsImport;
	private List<News> newsTick;
	private List<News> newsNotice;
	private List<News> newsNoticeImage;
	private Area important;
	private Area tinicNews;
	private Area notice;
	private Area area4;
	private List<FriendLink> outLink;
	private List<FriendLink> inLink;
	private String searchText;
	private Page<SearchResult> sResult;
	private List<Area> areas;
	private Long areaId;
	private Long id;
	private String ids;
	private Long lid;
	@Autowired
	private LinkApplication linkApplication;
	private List<News> newsArea4;
	private Page<News> mores;
	private String type;
	private Date sugestDate;
	

	
	public List<News> getNewsNoticeImage() {
		return newsNoticeImage;
	}

	public void setNewsNoticeImage(List<News> newsNoticeImage) {
		this.newsNoticeImage = newsNoticeImage;
	}

	public Date getSugestDate() {
		return sugestDate;
	}

	public void setSugestDate(Date sugestDate) {
		this.sugestDate = sugestDate;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Page<News> getMores() {
		return mores;
	}

	public void setMores(Page<News> mores) {
		this.mores = mores;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getLid() {
		return lid;
	}

	public void setLid(Long lid) {
		this.lid = lid;
	}

	public List<News> getNewsArea4() {
		return newsArea4;
	}

	public void setNewsArea4(List<News> newsArea4) {
		this.newsArea4 = newsArea4;
	}

	public Area getArea4() {
		return area4;
	}

	public void setArea4(Area area4) {
		this.area4 = area4;
	}

	public Long getAreaId() {
		return areaId;
	}

	public void setAreaId(Long areaId) {
		this.areaId = areaId;
	}

	public List<Area> getAreas() {
		return areas;
	}

	public void setAreas(List<Area> areas) {
		this.areas = areas;
	}

	@Override
	public String getParam() {
		return "searchText="+searchText+"&type="+type+"&areaId="+(areaId==null?"":areaId);
	}

	public Long lid(Long areaId, Long id){
		return newsApplication.getLid(areaId,id);
	}
	
	public Long rid(Long areaId, Long id){
		return newsApplication.getRid(areaId,id);
	}
	
	public Long top(Long areaId){
		return newsApplication.getTop(areaId);
	}
	
	public Page<SearchResult> getSResult() {
		return sResult;
	}

	public void setSResult(Page<SearchResult> result) {
		sResult = result;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public void setLinkApplication(LinkApplication linkApplication) {
		this.linkApplication = linkApplication;
	}

	public List<FriendLink> getOutLink() {
		return outLink;
	}

	public void setOutLink(List<FriendLink> outLink) {
		this.outLink = outLink;
	}

	public List<FriendLink> getInLink() {
		return inLink;
	}

	public void setInLink(List<FriendLink> inLink) {
		this.inLink = inLink;
	}

	public List<News> getNewsNotice() {
		return newsNotice;
	}

	public void setNewsNotice(List<News> newsNotice) {
		this.newsNotice = newsNotice;
	}

	public Area getNotice() {
		return notice;
	}

	public void setNotice(Area notice) {
		this.notice = notice;
	}

	public List<News> getNewsTick() {
		return newsTick;
	}
	public void setNewsTick(List<News> newsTick) {
		this.newsTick = newsTick;
	}
	public Area getImportant() {
		return important;
	}
	public void setImportant(Area important) {
		this.important = important;
	}
	public Area getTinicNews() {
		return tinicNews;
	}
	public void setTinicNews(Area tinicNews) {
		this.tinicNews = tinicNews;
	}
	public void setFrontApplication(FrontApplication frontApplication) {
		this.frontApplication = frontApplication;
	}
	public void setNewsApplication(NewsApplication newsApplication) {
		this.newsApplication = newsApplication;
	}
	public List<News> getNewsImport() {
		return newsImport;
	}
	public void setNewsImport(List<News> newsImport) {
		this.newsImport = newsImport;
	}
	public void setCategoryApplication(CategoryApplication categoryApplication) {
		this.categoryApplication = categoryApplication;
	}
	public List<JsonCategory> getCates() {
		return cates;
	}
	public void setCates(List<JsonCategory> cates) {
		this.cates = cates;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public String favorite(){
		mores=newsApplication.queryMyFavorite(getUserId(), currentPage, pageSize);
		List<Long> cs=null;
		if(!isSuperAdmin()){
			cs=getUserCateIds();
		}
		cates=categoryApplication.getCates(cs);
		return "favorite";
	}
	
	/**
	 * 删除我的收藏
	 * @return
	 */
	public String delFavorite(){
		try{
			if(StringUtils.isNotEmpty(ids)){
				String[] idArr=ids.split(",");
				newsApplication.batchDeleteFavorite(idArr);
				jsonResult="删除成功!";
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult="出错："+e.getMessage();
		}
		return JSON;
	}
	
}
